<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form name="formUser" ng-submit="$ctrl.save()" novalidate>
  <md-card class="gravitee-user-container">
    <md-card-title>
      <md-card-title-text>
        <span class="md-headline">User information</span>
        <span class="md-subhead" ng-bind="$ctrl.user.displayName"></span>
      </md-card-title-text>
      <md-card-title-media style="text-align: center;">
        <gravitee-image image-id="$ctrl.user.id" image="$ctrl.user.picture" image-url="$ctrl.getUserPicture()" image-border-radius="true"
            image-name="$ctrl.user.displayName"
            image-form="formUser"></gravitee-image>
      </md-card-title-media>
    </md-card-title>
    <md-card-content>
      <div layout="row">
        <ng-md-icon flex="10" icon="person"></ng-md-icon>
        <label>{{$ctrl.user.lastname || '-'}} {{$ctrl.user.firstname || '-'}}</label>
      </div>
      <div layout="row">
        <ng-md-icon flex="10" icon="contact_mail"></ng-md-icon>
        <label>{{$ctrl.user.email || '-'}}</label>
      </div>
      <div layout="row">
        <ng-md-icon flex="10" icon="lock">
          <md-tooltip>
            <span ng-repeat="auth in $ctrl.user.authorities">{{auth.authority}}{{$last ? '' : ',&nbsp'}}</span>
          </md-tooltip>
        </ng-md-icon>
        <label ng-repeat="role in $ctrl.user.roles">{{"[" + role.scope + "]&nbsp;" + role.name}}{{$last ? '' : '&nbsp;-&nbsp;'}}</label>
      </div>
    </md-card-content>
    <div>
      <md-button class="md-raised" ng-click="$ctrl.generateToken()">
        Generate new personal access token
      </md-button>
      <div class="gravitee-user-token">Tokens you have generated that can be used to access the Gravitee.io management API.</div>

      <md-table-container ng-show="$ctrl.tokens.length">
        <table md-table>
          <thead md-head>
          <tr md-row>
            <th md-column>Name</th>
            <th md-column>Created</th>
            <th md-column>Last use</th>
            <th md-column></th>
          </tr>
          </thead>
          <tbody md-body>
            <tr md-row ng-repeat="token in $ctrl.tokens">
              <td md-cell><span><b>{{token.name}}</b></span></td>
              <td md-cell><span><b>{{token.created_at | datetimeFilter}}</b></span></td>
              <td md-cell><span><b>{{token.last_use_at? (token.last_use_at | humanDateFilter) : 'never'}}</b></span></td>
              <td md-cell>
                <ng-md-icon ng-click="$ctrl.revoke(token)" icon="delete" size="24" title="Revoke token '{{token.name}}'"></ng-md-icon>
              </td>
            </tr>
          </tbody>
        </table>
      </md-table-container>
    </div>
    <md-card-actions layout="row" layout-align="end center">
      <md-button type="submit" ng-disabled="formUser.$pristine">Update</md-button>
      <md-button ng-click="$ctrl.cancel()" ng-disabled="formUser.$pristine">Cancel</md-button>
    </md-card-actions>
  </md-card>
</form>
